package com.wangzhixuan.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.wangzhixuan.commons.utils.ReadXmlUtil;
import com.wangzhixuan.service.WebUtilService;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Controller
@RequestMapping({"/util"})
public class WebUtilAction {
	@RequestMapping({"/json"})
	public void westToJson(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String param = request.getParameter("param");
		JSONArray json = JSONArray.fromObject(WebUtilService.east(param));
		PrintWriter out = response.getWriter();
		out.write(json.toString());
	}

	@RequestMapping({"/east"})
	public void eastToJson(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String devaddr = request.getParameter("devaddr");
		JSONObject json = JSONObject.fromObject(WebUtilService.west(devaddr));
		PrintWriter out = response.getWriter();
		out.write(json.toString());
	}

	@RequestMapping({"/login"})
	public String login(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String usn = request.getParameter("username");
		String pwd = request.getParameter("password");
		List list = WebUtilService.login(usn, pwd);
		request.removeAttribute("username");
		request.removeAttribute("password");
		if (list != null && list.size() > 0) {
			request.setAttribute("datagriddataurl", ReadXmlUtil.datagriddataurl);
			return "../../../index";
		} else {
			request.setAttribute("error", "账号或密码错误");
			return "../../../login";
		}
	}
}